package domein;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;


public class AutoTotaalDiensten {
	private static ArrayList<Persoon> personen = new ArrayList<Persoon>();
	private static HashMap<String, Account> accounts = new HashMap<String, Account>();	
	
	public synchronized static void registreer(Persoon aPersoon, Account anAccount) throws IllegalArgumentException{
		if(accounts.containsKey(aPersoon.getEmail())){
			throw new  IllegalArgumentException("Email al in gebruik.");
		}
		addPersoon(aPersoon);
		addAccount(aPersoon.getEmail(), anAccount);
	}
	
	public synchronized static void addPersoon(Persoon aPersoon){
		if(personen == null){
			personen = new ArrayList<Persoon>();
		}
		personen.add(aPersoon);		
	}

	public synchronized static Account addAccount(String email, Account anAccount){
		if(accounts == null){
			accounts = new HashMap<String, Account>();
		}
		return accounts.put(email, anAccount);
	}
	
	public synchronized static HashMap<String, Account> getAccounts(){
		return accounts;
	}
	
	public synchronized static ArrayList<Persoon> getPersonen(){
		return personen;
	}
	
	public synchronized static ArrayList<Klant> getKlanten() {
		ArrayList<Klant> tempList = new ArrayList<Klant>();
		for (Persoon p: personen) {
			if (p instanceof Klant){
				tempList.add((Klant)p);
			}
		}
		return tempList;
	}
	
	public synchronized static Persoon login(String email, String wachtwoord){
		Account account = accounts.get(email);
		if(account == null){
			return null;
		}else{
			Persoon p = account.getKlant(wachtwoord);
			if(p == null){
				return null;
			}
	
			return p;
		}
	}

	public synchronized static void setAccounts(HashMap<String, Account> a) {
		accounts = a;
		
	}
	
	public synchronized static void setPersonen(ArrayList<Persoon> p){
		personen = p;
	}
	
	public synchronized static Persoon getPersoon(int i){
		return personen.get(i);
	}
	
	public synchronized static Personeel getPersoneel(int i){
		if(!(personen.get(i) instanceof Personeel)){
			throw new ClassCastException(personen.get(i).getNaam() + " is geen personeel");
		}
		return (Personeel) personen.get(i);		
	}
		
	public static ArrayList<Personeel> getPersoneelbyFunctie(String functie){
		ArrayList<Personeel> tempList = new ArrayList<Personeel>();
		for(Persoon p : personen){
			if(p instanceof Personeel){
				if (((Personeel)p).getFunctie().equals(functie)){
					tempList.add((Personeel)p);
				}
			}
		}
		return tempList;
	}
}
